﻿using PHRClient.DoctorForms;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace PHRClient
{
    class Validator
    {
        public static Patient patient = null;

        public static void loginValidate(string username, string password)
        {

            patient = PatientLoader.getPatientByUserNameAndPassword(username, password);

            if (patient != null)
            {
                if (MainForm._loginForm.InvokeRequired)
                {
                    MainForm._loginForm.BeginInvoke((MethodInvoker)delegate()
                    {
                        MainForm._loginForm.Close();

                        MainForm._phrDetails = new PHRDetails(patient);
                        MainForm._phrDetails.Show();                        
                    });
                    
                }
                else
                {
                    MainForm._loginForm.Close();
                    MainForm._phrDetails = new PHRDetails(patient);
                    MainForm._phrDetails.Show();                   
                }
            }
            else
            {
                RegisterPatient registerPatient = new RegisterPatient();
                registerPatient.Show();
                MessageBox.Show("UserName Or Password Incorrect", "Please Register User", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        public static void loginValidate(string username)
        {
            patient = PatientLoader.getPatientByUserName(username);

            if (patient != null)            
            {
                if (MainForm._loginForm.InvokeRequired)
                {
                    MainForm._loginForm.BeginInvoke((MethodInvoker)delegate()
                    {
                        MainForm._loginForm.Close();

                        MainForm._phrDetails = new PHRDetails(patient);
                        MainForm._phrDetails.Show();

                    });
                }
                else
                {
                    MainForm._loginForm.Close();
                    MainForm._phrDetails = new PHRDetails(patient);
                    MainForm._phrDetails.Show();
                }
            }
            else
            {
                
                MessageBox.Show("UserName Or Password Incorrect", "Please Register User", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                if (MainForm._loginForm.InvokeRequired)
                {
                    MainForm._loginForm.BeginInvoke((MethodInvoker)delegate()
                    {
                        RegisterPatient registerPatient = new RegisterPatient();
                        registerPatient.Show();

                    });
                }
                else
                {
                    RegisterPatient registerPatient = new RegisterPatient();
                    registerPatient.Show();
                }
                
            }
        }

        public static void doctorLoginValidate(string username)
        {            
            if (username.Equals("Doctor", StringComparison.InvariantCultureIgnoreCase))
            {
                DoctorMainForm._loginForm.Close();
                DoctorMainForm._doctorDashboard = new DoctorDashboard();
                DoctorMainForm._doctorDashboard.Show();

            }
            else
            {
                MessageBox.Show("UserName Or Password Incorrect", "Login Error ", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }
    }
}
